﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BllFactory;
using Common;
using Controllers.Extend;
using Controllers.Filter;
using IBll;
using Model;
using Model.Other;
using Model.Sys;
using Model.Sys.Enum;

namespace Controllers.Controllers
{
    [Exception]
    public class HomeController : UserBaseController
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.cookieName = "UserInfo";
            base.OnAuthorization(filterContext);
            if (loginUser != null)
            {
                filterContext.Result = new RedirectResult("/UserInfo/Index");
            }
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string validateCode, string username, string password)
        {
            validateCode = validateCode.ToLower().Trim();
            string projectName = ConfigurationManager.AppSettings["ProjectName"] + "_";
            validateCode = projectName + validateCode;

            //最后判断来源
            if (CryptoHelper.MD5Encrypt(validateCode) == CookieHelper.GetCookieValue("validateCode"))
            {
                UserInfo userInfo = CurrentBllSession.UserInfoBll.GetUserInfoByUsernamePassword(username, password);
                if (userInfo == null)
                {
                    return Json(new ResultModel { Msg = "用户不存在", Pass = false });
                }
                else
                {
                    string cookieValue = ConfigurationManager.AppSettings["ProjectName"] + "_" + StringHelper.GetGuidString();
                    CookieHelper.SetCookie(base.cookieName, cookieValue);
                    //更新用户登录信息
                    userInfo.LastLoginTime = DateTime.Now;
                    userInfo.LastLoginIp = HttpHelper.GetClientIPAddress();
                    CurrentBllSession.UserInfoBll.Update(userInfo);
                    //设置loginUser
                    User user = GetUser(userInfo);
                    loginUser = user;
                    //加入缓存
                    CacheHelper.SetCache(Constant.CacheKey.LoginUserInfoCacheKey + "_" + cookieValue, user);
                    //清除验证码code
                    CookieHelper.ClearCookie("validateCode");
                    return Json(new ResultModel { Msg = "登陆成功", Pass = true });
                }
            }
            else
            {
                return Json(new ResultModel { Msg = "验证码错误", Pass = false });
            }

            return View();
        }

        private User GetUser(UserInfo userInfo)
        {
            User user = new User();

            user.UserId = userInfo.UserInfoId;
            user.LoginName = userInfo.LoginName;
            user.LoginPassword = userInfo.LoginPassword;
            user.Nickname = userInfo.Nickname;
            user.Headphoto = userInfo.Headphoto;
            user.Email = userInfo.Email;
            user.Phone = userInfo.Phone;
            user.QQ = userInfo.QQ;
            user.AddTime = userInfo.AddTime;
            user.LastLoginTime = (DateTime)userInfo.LastLoginTime;
            user.LastLoginIp = userInfo.LastLoginIp;

            return user;
        }

        [HttpGet]
        public void ValidateCode()
        {
            string validateCode = "";
            string projectName = ConfigurationManager.AppSettings["ProjectName"] + "_";
            string validateCodeEncrypt = "";

            Common.ValidateCode.CreateValidateGraphic(out validateCode, 4, 140, 40, 24);
            validateCodeEncrypt = CryptoHelper.MD5Encrypt(projectName + validateCode);//加密
            CookieHelper.SetCookie("validateCode", validateCodeEncrypt);
        }

    }
}
